// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.config;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.actions.RemoteActionInformation;
import pl.bristleback.server.bristle.exceptions.MissingPluginConfigurationElementException;
import pl.bristleback.server.bristle.utils.ResolverUtil;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Default implementation of {@link pl.bristleback.server.bristle.config.ActionAnnotationsProcessor} interface.
 * If no annotation processor is specified by user, this implementation will be used to retrieve annotated actions.
 * This processor checks every not abstract class in package named by given plugin configuration containing
 * {@link pl.bristleback.server.bristle.config.BristleConstants#ACTION_PACKAGE_SETTING_NAME} setting.
 * <p/>
 * Created on: 2010-09-03 16:05:22 <br/>
 *
 * @author Wojciech Niemiec
 */
public final class PackageActionAnnotationsProcessor implements ActionAnnotationsProcessor {
  private static Logger log = Logger.getLogger(PackageActionAnnotationsProcessor.class.getName());

  private String packageName;
  private List<RemoteActionInformation> actionsList;

  private ActionInformationResolver actionInformationResolver;

  public PackageActionAnnotationsProcessor() {
    actionsList = new ArrayList<RemoteActionInformation>();
    actionInformationResolver = new ActionInformationResolver();
  }

  public List<RemoteActionInformation> getAnnotatedActions(BristleConfiguration configuration) {
    packageName = (String) configuration.getSettings().get(BristleConstants.ACTION_PACKAGE_SETTING_NAME);
    if (StringUtils.isEmpty(packageName)) {
      throw new MissingPluginConfigurationElementException(BristleConstants.ACTION_PACKAGE_SETTING_NAME);
    }
    for (Class<? extends RemoteAction> actionClass : getActionClasses()) {
      initActionInformation(actionClass, configuration);
    }
    return actionsList;
  }

  private Set<Class<? extends RemoteAction>> getActionClasses() {
    String[] packages = StringUtils.split(packageName, '.');
    ResolverUtil<RemoteAction> resolver = new ResolverUtil<RemoteAction>();
    resolver.findImplementations(RemoteAction.class, packages);
    return resolver.getClasses();
  }

  private void initActionInformation(Class<? extends RemoteAction> actionClass, BristleConfiguration configuration) {
    if (Modifier.isAbstract(actionClass.getModifiers())) {
      return;
    }

    RemoteActionInformation information = actionInformationResolver.getActionInformation(actionClass, configuration);
    if (information == null) {
      return;
    }

    actionsList.add(information);
  }
}

